package com.weipai.demo.util;


import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 推送
 * @className JPushUtil   
 * @author gyt
 * 2017年9月13日 下午3:27:00
 */
public class JPushUtil {
	
	//设置推送参数
    private static final Logger log = LoggerFactory.getLogger(JPushUtil.class);
    private static String masterSecret = "119d9fa0a4e38cf739230590";
    private static String appKey = "fe96b92a2f4d9bc2813bad47";
    private static final String ALERT = "推送信息";    
    
    //安卓推送消息
    public static void jpushAndroid(Map<String, String> parm) {
        // 设置好账号的app_key和masterSecret 
        //创建JPushClient
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        //推送的关键,构造一个payload 
        PushPayload payload = PushPayload.newBuilder()
             .setAudience(Audience.all())//你项目中的所有用户
             .setNotification(Notification.android(parm.get("msg"), parm.get("atitle"), parm))//发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
             .setOptions(Options.newBuilder().setApnsProduction(false).build())//这里是指定开发环境,不用设置也没关系
             .setMessage(Message.content(parm.get("msg")))//自定义信息
             .build();
        try {
             PushResult pu = jpushClient.sendPush(payload);
         } catch (APIConnectionException e) {
             e.printStackTrace();
         } catch (APIRequestException e) {
             e.printStackTrace();
         }    
    } 
    
    //极光推送>>ios
    //Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数
    public static  void jpushIOS(Map<String, String> parm) {
        //创建JPushClient
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        PushPayload payload = PushPayload.newBuilder()
             .setPlatform(Platform.ios())//ios平台的用户
             .setAudience(Audience.all())//所有用户
             .setNotification(Notification.newBuilder()
             .addPlatformNotification(IosNotification.newBuilder()
                             .setAlert(parm.get("msg"))
                             .setBadge(+1)
                             .setSound("happy")
                             .addExtras(parm)
                             .build())
                     .build())
             .setOptions(Options.newBuilder().setApnsProduction(false).build())//指定IOS的生产环境，因为开发环境下，ios无法推送消息
             .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
             .build();

        try {
             PushResult pu = jpushClient.sendPush(payload);
         } catch (APIConnectionException e) {
             e.printStackTrace();
         } catch (APIRequestException e) {
             e.printStackTrace();
         }    
    }
    
    
//    /**
//     * 极光推送
//     */
//    public static void jiguangPush(){
//        String alias = "123456";//声明别名
//        log.info("对别名" + alias + "的用户推送信息");
//        PushResult result = push(String.valueOf(alias), ALERT);
//        if(result != null && result.isResultOK()){
//            log.info("针对别名" + alias + "的信息推送成功！");
//        }else{
//            log.info("针对别名" + alias + "的信息推送失败！");
//        }
//    }
//    
//    /**
//     * 极光推送方法(采用java SDK)
//     * @param alias 别名
//     * @param alert 推送消息
//     * @return PushResult
//     */
//    public static PushResult push(String alias, String alert){
//        ClientConfig clientConfig = ClientConfig.getInstance();
//        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
//        PushPayload payload = buildPushObject_android_ios_alias_alert(alias, alert);
//        try {
//            return jpushClient.sendPush(payload);
//        } catch (APIConnectionException e) {
//            log.error("Connection error. Should retry later. ", e);
//            return null;
//        } catch (APIRequestException e) {
//            log.error("Error response from JPush server. Should review and fix it. ", e);
//            log.info("HTTP Status: " + e.getStatus());
//            log.info("Error Code: " + e.getErrorCode());
//            log.info("Error Message: " + e.getErrorMessage());
//            log.info("Msg ID: " + e.getMsgId());
//            return null;
//        }    
//    }
//    
//    /**
//     * 生成极光推送对象PushPayload（采用java SDK）
//     * @param alias
//     * @param alert
//     * @return PushPayload
//     */
//    public static PushPayload buildPushObject_android_ios_alias_alert(String alias,String alert){
//        return PushPayload.newBuilder()
//                .setPlatform(Platform.android_ios())
//                .setAudience(Audience.alias(alias))
//                .setNotification(Notification.newBuilder()
//                        .addPlatformNotification(AndroidNotification.newBuilder()
//                                .addExtra("type", "infomation")
//                                .setAlert(alert)
//                                .build())
//                        .addPlatformNotification(IosNotification.newBuilder()
//                                .addExtra("type", "infomation")
//                                .setAlert(alert)
//                                .build())
//                        .build())
//                .setOptions(Options.newBuilder()
//                        .setApnsProduction(false)//true-推送生产环境 false-推送开发环境（测试使用参数）
//                        .setTimeToLive(90)//消息在JPush服务器的失效时间（测试使用参数）
//                        .build())
//                .build();
//    }
}
